package com.ete.lib.upload.ui

import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AppCompatDialog
import com.ete.lib.upload.bean.CommonDialogEntry
import com.ete.lib.upload.databinding.DialogCommonTipBinding
import com.ete.lib.upload.logZip.UploadLogUtil

/**
 * Author:LiXian
 * <p>
 * Desc:TODO
 * Version:1.0
 * date 2023/12/20 14:23
 */
class CommonDialog(var mDialogEntry: CommonDialogEntry)
    : AppCompatDialog(mDialogEntry.mContext, mDialogEntry.mStyle) {

    private val mBinding = DialogCommonTipBinding.inflate(LayoutInflater.from(context))

    fun initDialog() {
        if (!mDialogEntry.isOutTouchCancel) {
            setCanceledOnTouchOutside(false)
            setCancelable(false)
        }
        setContentView(mBinding.root)
        initView(mBinding)
    }

    private fun initView(mBinding: DialogCommonTipBinding) {
        mBinding.mEntry = mDialogEntry
        mBinding.dialogBtnCancel.setOnClickListener { dismiss() }
        mBinding.dialogIvClose.setOnClickListener {
            if (mDialogEntry.mCancelCallback == null) {
                dismiss()
            } else {
                val isDismiss: Boolean = mDialogEntry.mCancelCallback!!.invoke()
                if (isDismiss) dismiss()
            }
        }
        mBinding.dialogBtnOk.setOnClickListener {
            if (mDialogEntry.mSureCallback == null) {
                dismiss()
            } else {
                val isDismiss: Boolean = mDialogEntry.mSureCallback!!.invoke()
                if (isDismiss) dismiss()
            }
        }
    }

    fun updateContent(content: String) {
        mBinding.dialogTvContent.text = content
    }

    fun updateTitle(mTitle: String) {
        mBinding.dialogTvTitle.text = mTitle
    }

    fun showLoadingView() {
        mBinding.dialogLvLoading.visibility = View.VISIBLE
    }

    override fun onStart() {
        super.onStart()
        val width = UploadLogUtil.getDialogWidth(0.45F)
        if (window != null) {
            val mLayoutParams = window!!.attributes
            mLayoutParams.width = width
            mLayoutParams.height = mLayoutParams.height
            window!!.attributes = mLayoutParams
        }
    }

    override fun show() {
        super.show()
        UploadLogUtil.fullScreen(window)
    }
}